package com.redrose.videodemo.view;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.TextureView;

import com.redrose.videodemo.camera.CameraTextureActivity;
import com.redrose.videodemo.camera.CameraUtil;
import com.redrose.videodemo.camera.CameraUtil2;
import com.redrose.videodemo.camera.Preview;
import com.redrose.videodemo.utils.BitmapCallBack;
import com.redrose.videodemo.utils.LogUtils;
import com.redrose.videodemo.utils.ToastUtil;

import java.io.IOException;

/**
 * Desc: TextureView 预览Camera
 *
 * @author: RedRose
 * Date: 2019/4/16 0016
 * Email: yinsxi@163.com
 */
public class PreviewTextureView extends TextureView implements TextureView.SurfaceTextureListener,
        Camera.PictureCallback ,Camera.PreviewCallback{
    private static final String TAG = PreviewTextureView.class.getClass().getSimpleName();
    private BitmapCallBack mBitmapBack;
    private SurfaceTexture mSurfaceTexture;
    private Camera mCamera;

    public PreviewTextureView(Context context) {
        this(context, null);
    }

    public PreviewTextureView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PreviewTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSurfaceTextureListener(this);
        mCamera = CameraUtil2.getInstance().openCamera();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        LogUtils.d(TAG, "--onSurfaceTextureAvailable--");
        mSurfaceTexture = surface;
        if (CameraUtil2.getInstance().getIsRelease()) {
            mCamera = CameraUtil2.getInstance().openCamera();
        }
        CameraUtil2.getInstance().doStartPreview(surface);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        LogUtils.d(TAG, "--onSurfaceTextureSizeChanged--");
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        LogUtils.d(TAG, "--onSurfaceTextureDestroyed--");
        CameraUtil2.getInstance().releaseCamera();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        LogUtils.d(TAG, "--onSurfaceTextureUpdated--");

    }

    public void takePhoto() {
        if (mCamera != null) {
            mCamera.autoFocus(new Camera.AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean success, Camera camera) {
                    //设置聚焦成功后再拍照，其实可以不用。看需求吧，可以直接调用takePicture（）
                    //有些手机会聚焦失败，也就是success是false
                    if (success) {
                        camera.cancelAutoFocus();
                        mCamera.takePicture(null, null, PreviewTextureView.this);
                    }
                }
            });
        }
    }

    public void setBitmapCallback(BitmapCallBack bitmapCallback) {
        this.mBitmapBack = bitmapCallback;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        if (mBitmapBack != null) {
            mBitmapBack.backByte(data);
        }
        camera.startPreview();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /**
         * 触摸聚焦
         */
        mCamera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    ToastUtil.show(getContext(), "聚焦成功");
                    camera.cancelAutoFocus();
                }
            }
        });
        return super.onTouchEvent(event);
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        //这里就是预览返回的 NV21数据
    }
    private boolean isStartVideo = false;
    public void startVideo() {
        isStartVideo = false;
        ToastUtil.show(getContext(),"长按监听，开始录像");
    }
}
